<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-button type="primary" @click="addBtn()">添加分类</el-button>
      </el-row>
      <el-table :data="tableData" style="width: 100%" border row-key="cat_id">
        <el-table-column prop="cat_name" label="分类名称"></el-table-column>
        <el-table-column label="是否有效" width="180">
          <template slot-scope="icData">
            <i v-if="!icData.row.cat_deleted" class="el-icon-success" style="color:green"></i>
            <i v-else class="el-icon-error" style="color:red"></i>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序" width="180">
          <template slot-scope="stData">
            <el-tag v-if=" stData.row.cat_level ===0">一级</el-tag>
            <el-tag v-else-if="stData.row.cat_level ===1" type="success">二级</el-tag>
            <el-tag v-else type="danger">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <el-button width="180" size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button width="180" size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="yecan.num"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="yecan.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tot"
      ></el-pagination>
    </el-card>

    <!-- 添加分类的弹出框 -->
    <el-dialog title="添加分类" :visible.sync="adduser" width="50%" @close="resetFields()">
      <el-form label-width="80px" :model="addcan" ref="addform" :rules="rules" status-icon>
        <el-form-item label="活动名称" prop="cat_name">
          <el-input v-model="addcan.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类上级" prop="sltwoCat">
          <el-cascader
            v-model="addcan.sltwoCat"
            :options="options"
            :props="{
            expandTrigger: 'hover',
            checkStrictly:true,
            value:'cat_id',
            label:'cat_name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adduser = false">取 消</el-button>
        <el-button type="primary" @click=" addsure()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created () {
    // 一进页面就获得数据
    this.getTableList()
  },
  data () {
    return {
      tot: 0,
      yecan: {
        size: 5,
        num: 1
      },
      // 渲染表格
      TBcan: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      tableData: [],
      tableData2: [],

      // 添加分类
      adduser: false,
      options: [],
      addcan: {
        cat_name: '',
        cat_pid: '',
        cat_level: '',
        sltwoCat: []
      },
      rules: {
        cat_name: [{ required: true, message: '分类名称必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      console.log(val, 1)
      this.TBcan.pagesize = val
      this.getTableList()
    },
    handleCurrentChange (val) {
      console.log(val, 2)
      this.TBcan.pagenum = val
      this.getTableList()
    },
    handleChange () {
      // console.log(this.addcan.sltwoCat)
      var aa = this.addcan.sltwoCat
      // console.log(aa)

      if (aa.length === 0) {
        this.addcan.cat_level = 0
        this.addcan.cat_pid = 0
      } else if (aa.length === 1) {
        // 说明添加的是2级分类 我们就需要获一级分类的ID(就是数组的第一项数据) 就是分类父id
        this.addcan.cat_level = 1
        this.addcan.cat_pid = aa[0]
        console.log(this.cat_pid)
      } else {
        this.addcan.cat_level = 2
        this.addcan.cat_pid = aa[1]
      }
    },
    async addBtn () {
      // 显示添加分类弹框
      this.adduser = true
      // 发送请求获得级联动数据
      var { data: dt } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      // console.log(dt)
      // 赋值给options 接收1-2级别分类信息
      this.options = dt.data
      // console.log(this.options)
    },
    addsure () {
      this.$refs.addform.validate(async valid => {
        if (valid) {
          // console.log(this.addcan)

          var { data: dt } = await this.$http.post('categories', this.addcan)
          // console.log(dt)
          // 隐藏弹出框
          this.adduser = false
          // 提示小时
          this.$message.success(dt.meta.msg)
          // 重新渲染
          this.getTableList()
        }
      })
    },
    resetFields () {
      this.$refs.addform.resetFields()
      this.addcan.cat_level = 0
      this.addcan.cat_pid = 0
    },
    async getTableList () {
      var { data: dt } = await this.$http.get('categories', {
        params: this.TBcan
      })
      console.log(dt)
      // 把获得的数据赋值给tableData
      this.tableData = dt.data.result
      this.tot = dt.data.total
    }
  }
}
</script>

<style lang="less" scoped>
</style>
